package com.bing.bingPro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TTtInstallStatsProvinceDto;
import com.bing.bingPro.controller.vo.TTtInstallStatsCityVo;
import com.bing.bingPro.controller.vo.TTtInstallStatsProvinceVo;
import com.bing.bingPro.entity.TTtInstlStats;
import com.bing.bingPro.enums.TimeGranularityEnums;
import com.bing.bingPro.mapper.TTtInstlStatsMapper;
import com.bing.bingPro.service.TTtInstallStatsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.bing.bingPro.ReflectUtil.copyAndReplaceNullsWithDashes;

/**
 * @author 29821
 * @description 针对表【t_tt_instl_stats(铁通装机工单统计)】的数据库操作Service实现
 * @createDate 2023-05-19 14:13:33
 */
@Service
public class TTtInstallStatsServiceImpl extends ServiceImpl<TTtInstlStatsMapper, TTtInstlStats>
        implements TTtInstallStatsService {

    @Override
    public List<TTtInstallStatsCityVo> queryStatsProvince(TTtInstallStatsProvinceDto tOrderYearDto) {
        String time = tOrderYearDto.getTime().replace("-", "");
        //时间粒度为日
        if (TimeGranularityEnums.DAY.getCode().equals(tOrderYearDto.getTimeGranularity())) {
            return baseMapper.selectList(Wrappers
                            .lambdaQuery(TTtInstlStats.class)
                            .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getAreaType()), TTtInstlStats::getAreaType, tOrderYearDto.getAreaType())
                            .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getProvinceName()), TTtInstlStats::getProvinceName, tOrderYearDto.getProvinceName())
                            .eq(TTtInstlStats::getDateType, "day")
                            .eq(TTtInstlStats::getDateTime, time))
                    .stream()
                    .map(getTtInstallStatsTTtInstallStatsProvinceVoFunction(tOrderYearDto))
                    .collect(Collectors.toList());
        }
        //时间粒度为月
        return baseMapper.selectList(Wrappers.lambdaQuery(TTtInstlStats.class)
                        .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getAreaType()), TTtInstlStats::getAreaType, tOrderYearDto.getAreaType())
                        .eq(CharSequenceUtil.isNotEmpty(tOrderYearDto.getProvinceName()), TTtInstlStats::getProvinceName, tOrderYearDto.getProvinceName())
                        .eq(TTtInstlStats::getDateType, "month")
                        .eq(TTtInstlStats::getDateTime, time))
                .stream()
                .map(getTtInstallStatsTTtInstallStatsProvinceVoFunction(tOrderYearDto))
                .collect(Collectors.toList());

    }

    @Override
    public List<TTtInstallStatsCityVo> exportStatsCity(TTtInstallStatsProvinceDto tOrderYearDto) {
        List<TTtInstallStatsCityVo> tTtInstallStatsCityVos = this.queryStatsProvince(tOrderYearDto);
        return tTtInstallStatsCityVos.stream()
                .map(tTtInstallStatsCityVo -> {
                    TTtInstallStatsCityVo vo = new TTtInstallStatsCityVo();
                    BeanUtil.copyProperties(tTtInstallStatsCityVo,vo);
                    vo.setDateTime(tOrderYearDto.getTime());
                    return vo;
                })
                .collect(Collectors.toList());
    }

    private Function<TTtInstlStats, TTtInstallStatsCityVo> getTtInstallStatsTTtInstallStatsProvinceVoFunction(TTtInstallStatsProvinceDto tOrderYearDto) {
        return tTtInstallStats -> {
            TTtInstallStatsCityVo vo = new TTtInstallStatsCityVo();
            try {
                copyAndReplaceNullsWithDashes(tTtInstallStats, vo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            vo.setDateTime(tOrderYearDto.getTime());
            return vo;
        };
    }
}




